﻿using Lcygcc.Infrastructure.WindowService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lcygcc.WindowService.WindowSteup
{
    public partial class frm_ServiceManager : Form
    {
        private List<WindowsService> windowsServiceList = new List<WindowsService>();
        //private IContainer components;
        //private Button btn_Install;
        //private Button btn_Start;
        public frm_ServiceManager()
        {
            this.InitializeComponent();
        }
        private void frm_ServiceManager_Load(object sender, EventArgs e)
        {
            WindowsServiceManager windowsServiceManager = new WindowsServiceManager();
            this.windowsServiceList.Add(new WindowsService
            {
                Name = "Lcygcc.CenterService.DaemonService",
                ProcessName = "Lcygcc.CenterService.DaemonService",
                Description = "Lcygcc守护服务",
                Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lcygcc.WindowService.Daemon.exe")
            });
            this.windowsServiceList.Add(new WindowsService
            {
                Name = "Lcygcc.CenterService.Host",
                ProcessName = "Lcygcc.CenterService.Host",
                Description = "Lcygcc中心服务",
                Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lcygcc.WindowService.Host.exe")
            });
            foreach (WindowsService current in this.windowsServiceList)
            {
                if (!windowsServiceManager.ServiceIsExist(current))
                {
                    this.btn_Install.Text = "安装服务";
                    this.btn_Start.Enabled = false;
                    break;
                }
                this.btn_Install.Text = "卸载服务";
                this.btn_Start.Enabled = true;
            }
            foreach (WindowsService current2 in this.windowsServiceList)
            {
                if (!windowsServiceManager.ServiceIsRunning(current2))
                {
                    this.btn_Start.Text = "启动服务";
                    break;
                }
                this.btn_Start.Text = "停止服务";
                this.btn_Install.Enabled = false;
            }
        }
        private void btn_Install_Click(object sender, EventArgs e)
        {
            WindowsServiceManager windowsServiceManager = new WindowsServiceManager();
            if (this.btn_Install.Text == "安装服务")
            {
                foreach (WindowsService current in this.windowsServiceList)
                {
                    if (windowsServiceManager.ServiceIsExist(current))
                    {
                        windowsServiceManager.UninstallService(current);
                    }
                    windowsServiceManager.InstallService(current);
                }
                for (int i = 0; i < 10; i++)
                {
                    bool flag = true;
                    foreach (WindowsService current2 in this.windowsServiceList)
                    {
                        flag = (windowsServiceManager.ServiceIsExist(current2) && flag);
                    }
                    if (flag)
                    {
                        this.btn_Install.Text = "卸载服务";
                        this.btn_Start.Enabled = true;
                        MessageBox.Show("服务安装完成");
                        return;
                    }
                    if (i == 9)
                    {
                        MessageBox.Show("服务安装异常");
                        return;
                    }
                    Thread.Sleep(500);
                    Application.DoEvents();
                }
                return;
            }
            foreach (WindowsService current3 in this.windowsServiceList)
            {
                if (windowsServiceManager.ServiceIsExist(current3))
                {
                    windowsServiceManager.UninstallService(current3);
                }
            }
            for (int j = 0; j < 10; j++)
            {
                bool flag2 = true;
                foreach (WindowsService current4 in this.windowsServiceList)
                {
                    flag2 = (!windowsServiceManager.ServiceIsExist(current4) && flag2);
                }
                if (flag2)
                {
                    this.btn_Install.Text = "安装服务";
                    this.btn_Start.Enabled = false;
                    this.btn_Start.Text = "启动服务";
                    MessageBox.Show("服务卸载完成");
                    return;
                }
                if (j == 9)
                {
                    MessageBox.Show("服务安装异常");
                    return;
                }
                Thread.Sleep(500);
                Application.DoEvents();
            }
        }
        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (this.btn_Start.Text == "启动服务")
            {
                try
                {
                    WindowsServiceManager windowsServiceManager = new WindowsServiceManager();
                    foreach (WindowsService current in this.windowsServiceList)
                    {
                        windowsServiceManager.StartService(current);
                    }
                    for (int i = 0; i < 15; i++)
                    {
                        bool flag = true;
                        foreach (WindowsService current2 in this.windowsServiceList)
                        {
                            flag = (windowsServiceManager.ServiceIsRunning(current2) && flag);
                        }
                        if (flag)
                        {
                            this.btn_Start.Text = "停止服务";
                            this.btn_Install.Enabled = false;
                            MessageBox.Show("服务已启动");
                            break;
                        }
                        if (i == 14)
                        {
                            MessageBox.Show("服务启动异常");
                            break;
                        }
                        Thread.Sleep(500);
                        Application.DoEvents();
                    }
                    return;
                }
                catch (Exception)
                {
                    return;
                }
            }
            try
            {
                WindowsServiceManager windowsServiceManager2 = new WindowsServiceManager();
                foreach (WindowsService current3 in this.windowsServiceList)
                {
                    windowsServiceManager2.StopService(current3);
                }
                for (int j = 0; j < 15; j++)
                {
                    bool flag2 = true;
                    foreach (WindowsService current4 in this.windowsServiceList)
                    {
                        flag2 = (!windowsServiceManager2.ServiceIsRunning(current4) && flag2);
                    }
                    if (flag2)
                    {
                        this.btn_Start.Text = "启动服务";
                        MessageBox.Show("服务已停止");
                        this.btn_Install.Enabled = true;
                        break;
                    }
                    if (j == 14)
                    {
                        MessageBox.Show("服务停止异常");
                        break;
                    }
                    Thread.Sleep(500);
                    Application.DoEvents();
                }
            }
            catch (Exception)
            {
            }
        }

    }
}
